package edu.gatech.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import edu.gatech.datastorage.Gender;
import edu.gatech.presenters.EditPatientPresenter;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.User;

public class EditPatientPanel {
	private static JTextField txtName;
	private static JTextField txtPassword;
	private static JTextField txtAge;
	private static JComboBox comboBox;
	private static JTextField txtPhoneNumber;
	private static JTextField txtAddress;
	private static JTextField txtPharmacy;
	private static JTextField txtInsurance;
	private static JTextField txtMedications;
	private static JTextField txtAllergies;
	private static Patient currentPatient;
	private static JFrame editPatient;
	private JTextField txtDateOfBirth;
	private EditPatientPresenter presenter;
	private static User currentUser;
	public EditPatientPanel(Patient curPat, User curUser){
		currentPatient = curPat;
		currentUser = curUser;
		editPatient = new JFrame("Edit Patient Panel");
		MenuBar menuBar = new MenuBar(editPatient);
		
		editPatient.setSize(600,480);
		editPatient.getContentPane().setLayout(null);
		
		Gender[]  gender = {Gender.MALE, Gender.FEMALE};
		ArrayList<String> meds = (ArrayList<String>) currentPatient.getInfo().getMedication();
		String medications = "";
		for(int i = 0; i < meds.size(); i++){
			medications = medications + meds.get(i);
			if(i != (meds.size() - 1)){
				medications = medications + ", ";
			}
		}
		ArrayList<String> alrgs = (ArrayList<String>) currentPatient.getInfo().getAllergies();
		String allergies = "";
		for(int i = 0; i < alrgs.size(); i++){
			allergies = allergies + alrgs.get(i);
			if(i != (alrgs.size() - 1)){
				allergies = allergies + ", ";
			}
		}
		
		JButton btnEditPatient = new JButton("Confirm");
		btnEditPatient.setFont(new Font("Arial", Font.PLAIN, 10));
		btnEditPatient.setBounds(295, 305, 76, 23);
		btnEditPatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter = new EditPatientPresenter(currentPatient);
				presenter.edit(txtMedications.getText(), txtAllergies.getText(),
						txtName.getText(), txtPassword.getText(), txtAge.getText(), 
						(Gender)comboBox.getSelectedItem(), txtPhoneNumber.getText(), txtAddress.getText(), 
						txtPharmacy.getText(), txtInsurance.getText(), txtDateOfBirth.getText());
			}
		});
		editPatient.getContentPane().add(btnEditPatient);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				editPatient.hide();
			}
		});
		btnCancel.setFont(new Font("Arial", Font.PLAIN, 10));
		btnCancel.setBounds(385, 305, 76, 23);
		editPatient.getContentPane().add(btnCancel);
		
		JLabel lblCurrentPatient = new JLabel("Current Patient:");
		lblCurrentPatient.setFont(new Font("Arial", Font.PLAIN, 10));
		lblCurrentPatient.setBounds(130, 57, 100, 14);
		editPatient.getContentPane().add(lblCurrentPatient);
		
		JLabel label = new JLabel("");
		label.setBounds(191, 155, 46, 14);
		editPatient.getContentPane().add(label);
		
		JLabel lblcurrentlySelectPatient = new JLabel(currentPatient.getInfo().getName());
		lblcurrentlySelectPatient.setFont(new Font("Arial", Font.BOLD, 14));
		lblcurrentlySelectPatient.setBounds(130, 77, 224, 14);
		editPatient.getContentPane().add(lblcurrentlySelectPatient);
		
		txtDateOfBirth = new JTextField();
		txtDateOfBirth.setText(currentPatient.getInfo().getDateOfBirth());
		txtDateOfBirth.setBounds(119, 242, 166, 21);
		editPatient.getContentPane().add(txtDateOfBirth);
		txtDateOfBirth.setColumns(10);
		
		txtName = new JTextField();
		txtName.setBounds(119, 146, 166, 21);
		editPatient.getContentPane().add(txtName);
		txtName.setFont(new Font("Arial", Font.PLAIN, 10));
		txtName.setText(currentPatient.getName());
		txtName.setColumns(10);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(119, 178, 166, 21);
		editPatient.getContentPane().add(txtPassword);
		txtPassword.setFont(new Font("Arial", Font.PLAIN, 10));
		txtPassword.setText(currentPatient.getPassword());
		txtPassword.setColumns(10);
		
		txtAge = new JTextField();
		txtAge.setBounds(119, 210, 166, 21);
		editPatient.getContentPane().add(txtAge);
		txtAge.setFont(new Font("Arial", Font.PLAIN, 10));
		txtAge.setText(currentPatient.getInfo().getAge() + "");
		txtAge.setColumns(10);
		
		comboBox = new JComboBox(gender);
		comboBox.setBounds(119, 274, 166, 21);
		editPatient.getContentPane().add(comboBox);
		comboBox.setSelectedItem(currentPatient.getInfo().getGender());
		comboBox.setFont(new Font("Arial", Font.PLAIN, 10));
		
		txtPhoneNumber = new JTextField();
		txtPhoneNumber.setBounds(119, 306, 166, 21);
		editPatient.getContentPane().add(txtPhoneNumber);
		txtPhoneNumber.setFont(new Font("Arial", Font.PLAIN, 10));
		txtPhoneNumber.setText(currentPatient.getInfo().getPhoneNumber());
		txtPhoneNumber.setColumns(10);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(295, 146, 166, 21);
		editPatient.getContentPane().add(txtAddress);
		txtAddress.setFont(new Font("Arial", Font.PLAIN, 10));
		txtAddress.setText(currentPatient.getInfo().getAddress());
		txtAddress.setColumns(10);
		
		txtPharmacy = new JTextField();
		txtPharmacy.setBounds(295, 178, 166, 21);
		editPatient.getContentPane().add(txtPharmacy);
		txtPharmacy.setFont(new Font("Arial", Font.PLAIN, 10));
		txtPharmacy.setText(currentPatient.getInfo().getPharmacy());
		txtPharmacy.setColumns(10);
		
		txtInsurance = new JTextField();
		txtInsurance.setBounds(295, 210, 166, 21);
		editPatient.getContentPane().add(txtInsurance);
		txtInsurance.setFont(new Font("Arial", Font.PLAIN, 10));
		txtInsurance.setText(currentPatient.getInfo().getInsurance());
		txtInsurance.setColumns(10);
		
		txtMedications = new JTextField();
		txtMedications.setBounds(295, 242, 166, 21);
		editPatient.getContentPane().add(txtMedications);
		txtMedications.setFont(new Font("Arial", Font.PLAIN, 10));
		txtMedications.setText(medications);
		txtMedications.setColumns(10);
		
		txtAllergies = new JTextField();
		txtAllergies.setBounds(295, 274, 166, 21);
		editPatient.getContentPane().add(txtAllergies);
		txtAllergies.setFont(new Font("Arial", Font.PLAIN, 10));
		txtAllergies.setText(allergies);
		txtAllergies.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Edit Patient", TitledBorder.LEADING, TitledBorder.TOP, null, UIManager.getColor("Button.darkShadow")));
		panel.setBounds(103, 122, 374, 221);
		editPatient.getContentPane().add(panel);
		
		JButton btnDeletePatient = new JButton("Delete Patient");
		btnDeletePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter = new EditPatientPresenter(currentPatient);
				presenter.deletePattient(currentUser);
				editPatient.hide();
			}
		});
		btnDeletePatient.setFont(new Font("Arial", Font.PLAIN, 10));
		btnDeletePatient.setBounds(295, 360, 166, 23);
		editPatient.getContentPane().add(btnDeletePatient);
		
		editPatient.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		editPatient.setSize(600,480);
		editPatient.setTitle("Edit Patient");
		editPatient.setLocationRelativeTo(null);  
        editPatient.setVisible(true);
	}
}
